var enemies = [];
var ENEMIES_TOTAL = 5;
const ENEMY_VELOCITY = 1;
var enemiesCourseTimer = 0;

const BLOCK_SIDE = 32;
const BLOCKS_IN_LVL = 20;

const FPS = 50;
const MAX_TIME = Math.round( FPS * 1.5 );
const VISIBILITY = 3 * BLOCK_SIDE;
var isPlaying = true;
var gameOver = false;

const LEFT_ARROW = 37;
const RIGHT_ARROW = 39;
const UP_ARROW = 38;
const DOWN_ARROW = 40;
const SPACE = 32;
const ESC = 27;
const ENTER = 13;
const keyW = 87;
const keyA = 65;
const keyS = 83;
const keyD = 68;

const TANK_HEIGHT = 32;
const TANK_WIDTH = 26;
const MAX_LIFES = 2;
const MAX_FIRE_RATE = Math.round( FPS / 30 );
const TANK_VELOCITY = 3;

const ROCKET_HEIGHT = 12;
const ROCKET_WIDTH = 6;
const ROCKET_VELOCITY = 6;
var rockets = [];

var g_timeHours = 0;
var g_timeMinutes = 0;
var g_timeSeconds = 0;
var g_timer = 0;

const addRange = Math.round( ( TANK_HEIGHT - TANK_WIDTH ) / 2 );

function OTank( x, y, type, velocity, img1, img2 )
{
    this.dX = 0;
    this.dY = 0;
    this.x = x;
    this.y = y;
    this.width = TANK_WIDTH;
    this.height = TANK_HEIGHT;
    this.velocity = velocity;
    this.lifes = MAX_LIFES;
    this.direction = "up";
    this.intelligence = "type1";
    this.type = type;
    if ( this.type == 0 )
    {
        this.lifes = ENEMIES_TOTAL * 3;
    }
    else
    {
        this.lifes = MAX_LIFES;
    }
    if ( this.type == 0 )
    {
        this.rocketsTimer = 0;
    }
    else
    {
        this.rocketsTimer = -45 * MAX_FIRE_RATE;
    }
    
    this.dead = false;
    this.img1 = img1;
    this.img2 = img2;
    this.alphaChannel = 0.0;
    this.currAnim = 'move';                   // текущая анимация
    this.frame = 0;                           // текущий кадр анимации
    this.anims = 
    {
        'move':                               // анимация движения
        {
            'img': this.img1,                 // картинка с анимацией
            'frames': 2,                      // количество кадров анимации, начиная с нуля
            'width': TANK_WIDTH,
            'height': TANK_HEIGHT,
            'speed': 3,                       // количество обновлений холста, необходимое для смены кадра
            'timer': 0                        // текущее обновление по счету
        },
        'dead':                               // анимация взрыва
        {
            'img': this.img2,
            'frames': 10,
            'width': 32,
            'height': 32,
            'speed': 2,
            'timer': 0
        }
    }

    this.pursue_enemies = false;

    switch (this.type)
    {
        case 0:
            this.fireRate = MAX_FIRE_RATE * 15;
        break;
        case 1:
            this.fireRate = MAX_FIRE_RATE * 35;
        break;
    }
    this.turn = function( newDirection )
    {
        var allowTurn = true;
        var lastX = this.x;
        var lastY = this.y;
        var lastDir = this.direction;
        
        this.doTurn( newDirection );
        if ( this != tank )
        {
            if ( checkCollision( tank, this, "tank" ))
            {
                allowTurn = false;
            }
        }
        else
        {
            for ( var i = 0; i < enemies.length; i++ )
            {
                if ( checkCollision( tank, enemies[i], "tank" ))
                {
                    tank.x -= tank.dX;
                    tank.y -= tank.dY;
                    enemies[i].x -= enemies[i].dX;
                    enemies[i].y -= enemies[i].dY;
                    checkWalls( tank, "tank" );
                    checkWalls( enemies[i], "tank" );
                    allowTurn = false;
                }
            }
        }
        
        if ( !allowTurn )
        {
            this.doTurn( lastDir );
            this.x = lastX;
            this.y = lastY;
        }
    }
    this.doTurn = function( newDirection )
    {
        switch ( newDirection )
        {
            case "left": 
                this.dX = -this.velocity;
                this.dY = 0;
                switch ( this.direction )
                {
                    case "up":
                        this.x -= Math.abs( TANK_HEIGHT - TANK_WIDTH );
                    break;
                    case "down":
                        this.x -= Math.abs( TANK_HEIGHT - TANK_WIDTH );
                        this.y += Math.abs( TANK_HEIGHT - TANK_WIDTH );
                    break;
                }
                this.width = TANK_HEIGHT;
                this.height = TANK_WIDTH;
            break;
            case "right":
                this.dX = this.velocity;
                this.dY = 0;
                switch ( this.direction )
                {
                    case "down":
                        this.y += Math.abs( TANK_HEIGHT - TANK_WIDTH );
                    break;
                }
                this.width = TANK_HEIGHT;
                this.height = TANK_WIDTH;
            break;
            case "up":
                this.dY = -this.velocity;
                this.dX = 0;
                switch ( this.direction )
                {
                    case "right":
                        this.x += Math.abs( TANK_HEIGHT - TANK_WIDTH );
                        this.y -= Math.abs( TANK_HEIGHT - TANK_WIDTH );
                    break;
                    case "left":
                        this.y -= Math.abs( TANK_HEIGHT - TANK_WIDTH );
                    break;
                 }
                this.width = TANK_WIDTH;
                this.height = TANK_HEIGHT;
            break;
            case "down":
                this.dY = this.velocity;
                this.dX = 0;
                switch ( this.direction )
                {
                    case "right":
                        this.x += Math.abs( TANK_HEIGHT - TANK_WIDTH );
                    break;
                }
                this.width = TANK_WIDTH;
                this.height = TANK_HEIGHT;
            break;
        }
        
        this.direction = newDirection;
    }
    this.launchRocket = function()
    {
        if ( !this.dead )
        {
            this.rocketsTimer++;
            if ( this.rocketsTimer > this.fireRate )
            {
                this.rocketsTimer = 0;
                switch (this.direction)
                {
                    case "up":
                        rockets[rockets.length] = new Rocket( this.x + this.width/2 - ROCKET_WIDTH/2, this.y - ROCKET_HEIGHT, 0, -ROCKET_VELOCITY, ROCKET_WIDTH, ROCKET_HEIGHT, this.type, g_rocketImg, g_rocketExplosionImg );
                    break;
                    case "right":
                        rockets[rockets.length] = new Rocket( this.x + this.width, this.y + this.width/2 - ROCKET_HEIGHT/2, ROCKET_VELOCITY, 0, ROCKET_HEIGHT, ROCKET_WIDTH, this.type, g_rocketImg, g_rocketExplosionImg );
                    break;
                    case "down":
                        rockets[rockets.length] = new Rocket( this.x + this.width/2 - ROCKET_WIDTH/2, this.y + this.height, 0, ROCKET_VELOCITY, ROCKET_WIDTH, ROCKET_HEIGHT, this.type, g_rocketImg, g_rocketExplosionImg );
                    break;
                    case "left":
                        rockets[rockets.length] = new Rocket( this.x - ROCKET_HEIGHT, this.y + this.width/2 - ROCKET_HEIGHT/2, -ROCKET_VELOCITY, 0, ROCKET_HEIGHT, ROCKET_WIDTH, this.type, g_rocketImg, g_rocketExplosionImg );
                    break;
                }
            }
        }
    }
    this.draw = drawTank
}

function Rocket( x, y, dX, dY, width, height, type, img1, img2 )
{
    this.dX = dX;
    this.dY = dY;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.type = type;
    this.dead = false;
    this.velocity = ROCKET_VELOCITY;
    this.img1 = img1;
    this.img2 = img2;
    this.currAnim = 'move';
    this.frame = 0;
    this.anims = 
    {
        'move':                            // анимация полета
        {
            'img': this.img1,
            'frames': 2,
            'width': ROCKET_WIDTH,
            'height': ROCKET_HEIGHT,
            'speed': 3,
            'timer': 0
        },
        'dead':                            // анимация взрыва
        {
            'img': this.img2,
            'frames': 9,
            'width': 12,
            'height': 12,
            'speed': 2,
            'timer': 0
          
        }
    }
    if ( this.dX > 0 )
    {
        this.direction = "right";
    }
    else if ( this.dX < 0 )
    {
        this.direction = "left";
    }
    if ( this.dY > 0 )
    {
        this.direction = "down";
    }
    else if ( this.dY < 0 )
    {
        this.direction = "up";
    }
    this.draw = drawRocket;
}

function Block( x, y, type )
{
    this.x = x;
    this.y = y;
    this.type = type;
    this.width = BLOCK_SIDE;
    this.height = BLOCK_SIDE;
    this.draw = drawBlock;
}